package cn.rkylin.oms.sysOrder.controller;

import cn.rkylin.oms.common.utils.LogUtil;
import cn.rkylin.oms.sysOrder.service.ISysOrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统订单控制器
 *
 * @author wangxing
 * @version 1.0
 * @created 2017.09.14
 */
@Controller
@RequestMapping("/sysOrderManager")
public class SysOrderController {
    private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * 系统订单服务层
     */
    @Autowired
    private ISysOrderService sysOrderService;

    /**
     * 日志类
     */
    @Autowired
    private LogUtil logUtil;

    /**
     * 生成系统订单
     *
     * @param shopId    店铺id
     * @param start     上次任务结束时间
     * @param end       本次任务开始时间
     * @param buyerNick 买家昵称 客户ID（手动下载用）
     * @param tradeCode 平台交易号（手动下载用）
     */
    @ResponseBody
    @RequestMapping(value = "/generateSysOrder", method = RequestMethod.GET)
    public Map<String, Object> generateSysOrder(String shopId, String start, String end, String buyerNick, String tradeCode) throws Exception {
//        logUtil.info(new DBLog(shopId, "系统订单自动下载", "跟踪日志", "系统订单自动下载任务controller开始start:" + start + ";end:" + end + "---------------------------------------"));

        /*
        //校验任务开始和结束时间
        if (StringUtils.isBlank(end)) {
            Date dt = new Date();
            end = simpleDateFormat.format(dt);
        }
        if (StringUtils.isEmpty(start) && StringUtils.isNotEmpty(end)) {
            Calendar c = Calendar.getInstance();
            c.setTime(simpleDateFormat.parse(end));
            c.add(Calendar.MONTH, -1);//提前一个月
            // 没有开始时间，按结束时间前一个月处理
            start = simpleDateFormat.format(c.getTime());
        }
        start = URLDecoder.decode(start, "UTF-8");//用于系统订单更新功能用
        end = URLDecoder.decode(end, "UTF-8");
        */

        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap();
        int result = 0;
        if (StringUtils.isNotBlank(shopId)) {
            result = sysOrderService.generateSysOrder(shopId, buyerNick, tradeCode);
        } else {
            returnMap.put("result", "shopId is null");
        }

        if (result <= 0) {
            returnMap.put("success", "false");
        } else {
            returnMap.put("success", "true");
        }

//        logUtil.info(new DBLog(shopId, "系统订单自动下载", "跟踪日志", "系统订单自动下载任务controller结束start:" + start + ";end:" + end + "---------------------------------------"));
        // 生成返回结果json串，null内容也需要返回
        return returnMap;
    }

    /**
     * 自动审核分派
     *
     * @param shopId 店铺id
     */
    @ResponseBody
    @RequestMapping(value = "/verifyAndAssignOrder", method = RequestMethod.GET)
    public Map<String, Object> verifyAndAssignOrder(String shopId) throws Exception {
//        logUtil.info(new DBLog(shopId, "系统订单自动审核分派", "跟踪日志", "系统订单自动审核分派任务controller开始---------------------------------------"));
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap();
        int result = 0;
        if (StringUtils.isNotBlank(shopId)) {
            result = sysOrderService.verifyAndAssignOrder(shopId);
        } else {
            returnMap.put("result", "shopId is null");
        }

        if (result <= 0) {
            returnMap.put("success", "false");
        } else {
            returnMap.put("success", "true");
        }
        // 生成返回结果json串，null内容也需要返回
//        logUtil.info(new DBLog(shopId, "系统订单自动审核分派", "跟踪日志", "系统订单自动审核分派任务controller结束---------------------------------------"));
        return returnMap;
    }

}